package com.gzucm.Scholarship.config.runner;

import com.gzucm.Scholarship.common.enums.SystemKind;
import com.gzucm.Scholarship.common.enums.SystemState;
import com.gzucm.Scholarship.entity.SystemValue;
import com.gzucm.Scholarship.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//@Component
public class InitAppRunner implements ApplicationRunner {

    @Autowired
    private SystemService systemService;

    @Override
    @Transactional
    public void run(ApplicationArguments args){
        List<SystemValue> timerList = systemService.findByKind(SystemKind.SYSTEM_STATE_TIMER);
        if (timerList.isEmpty()) {
            List<SystemValue> valueList = new ArrayList<>();
            SystemValue timer = new SystemValue();
            timer.setKind(SystemKind.SYSTEM_STATE_TIMER.getKind());
            timer.setCode(SystemState.OPEN_UP.name());
            valueList.add(timer);

            timer = new SystemValue();
            timer.setKind(SystemKind.SYSTEM_STATE_TIMER.getKind());
            timer.setCode(SystemState.EXAMINE.name());
            valueList.add(timer);

            timer = new SystemValue();
            timer.setKind(SystemKind.SYSTEM_STATE_TIMER.getKind());
            timer.setCode(SystemState.REPORT.name());
            valueList.add(timer);

            timer = new SystemValue();
            timer.setKind(SystemKind.SYSTEM_STATE_TIMER.getKind());
            timer.setCode(SystemState.SUSPEND.name());
            valueList.add(timer);

            systemService.save(valueList);

        }
    }
}
